package com.ruoyi.project.tool;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

/**
 * 阿里云短信发送工具
 */
public class SmsUtil {

    /**
     * 创建短信Client，自动从环境变量获取AK/SK
     */
    public static Client createClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId("LTAI5tGaQxmSKUk3yhGyynsP")
                .setAccessKeySecret("qFusZH0os1UmL7VJKIomkmy7SsGu4d");
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    /**
     * 发送短信
     *
     * @param phoneNumbers  手机号，多个用逗号隔开
     * @param signName      签名
     * @param templateCode  模板CODE
     * @param templateParam 模板参数JSON字符串，如{"code":"1234"}
     * @return SendSmsResponse短信接口响应体
     * @throws Exception
     */
    public static SendSmsResponse sendSms(String phoneNumbers, String signName, String templateCode, String templateParam) throws Exception {
        Client client = createClient();
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(phoneNumbers)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam(templateParam);
        return client.sendSms(request);
    }

    /**
     * 命令行用法：java com.aliyun.sample.SmsUtil 手机号 签名 模板CODE 模板参数json
     */
}